package com.lanou.moving.base;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;
import android.preference.PreferenceManager;

import com.lanou.moving.utils.MyConfig;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by 刘琪 on 15/11/3.
 */
public class BaseApplication extends Application {
    public static boolean mIsSleepClockSetting = false;//睡眠
    private static final String PARAM_IS_NIGHT_MODE = "PARAM_IS_NIGHT_MODE";
    //运用list来保存们每一个activity是关键
    private List<Activity> mList = new LinkedList<Activity>();
    private static Context context;
    private static BaseApplication instance;
    private SharedPreferences mSp;
    private boolean mIsNightMode;
    private static String rootPath = "/mymusic";
    public static String lrcPath = "/lrc";
    public static boolean isShake = true;





   //定义一个变量
    private static int mode;

    public static int getMode() {
        return mode;
    }

    public static void setMode(int mode) {
        BaseApplication.mode = mode;
    }





    public synchronized static BaseApplication getInstance() {
        if (null == instance) {
            instance = new BaseApplication();
        }
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        context = getApplicationContext();

        mode =0;


        mSp = PreferenceManager.getDefaultSharedPreferences(this);
        mIsNightMode = mSp.getBoolean(PARAM_IS_NIGHT_MODE, false);
        initPath();
    }

    public boolean isNightMode() {
        return mIsNightMode;
    }

    public void setIsNightMode(boolean isNightMode) {
        if (mIsNightMode == isNightMode)
            return;

        mIsNightMode = isNightMode;
        mSp.edit().putBoolean(PARAM_IS_NIGHT_MODE, mIsNightMode).apply();
        if (context == null)
            context = getApplicationContext();
    }


    public static Context getAppContext() {
        return context;
    }

    // add Activity
    public void addActivity(Activity activity) {
        mList.add(activity);
    }

    //关闭每一个list内的activity
    public void exit() {
        try {
            for (Activity activity : mList) {
                if (activity != null)
                    activity.finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.exit(0);
        }
    }

    //杀进程
    public void onLowMemory() {
        super.onLowMemory();
        System.gc();
    }

    private void initPath() {
        String ROOT = "";
        if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            ROOT = Environment.getExternalStorageDirectory().getPath();
        }
        rootPath = ROOT + rootPath;
        lrcPath = rootPath + lrcPath;
        File lrcFile = new File(lrcPath);
        if(lrcFile.exists()) {
            lrcFile.mkdirs();
        }
    }
}
